package com.platform.cdcs.model;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by holytang on 2017/10/12.
 */
public class ProductList extends MockObj implements Serializable {

    private List<ProductItem> disProductList;
    private List<ProductItem> productList;

    //
    public List<ProductItem> getProductList() {
        return productList;
    }

    public void setProductList(List<ProductItem> productList) {
        this.productList = productList;
    }

    public List<ProductItem> getDisProductList() {
        return disProductList;
    }

    public void setDisProductList(List<ProductItem> disProductList) {
        this.disProductList = disProductList;
    }

    public static class ProductItem implements Serializable {

        private String code;
        private String license_no;
        private String id;
        private String batchNumber;
        //数量
        private String qty = "";
        //单位
        private String uom = "";
        //产品代码
        private String itemCode;
        //产品名称
        private String itemName;

        private String serialNumber = "";
        private String pkg = "";
        private String status;
        private String materialName = "";
        private String material_name;
        private String materialNumber = "";
        private String saleUnit;
        private String productNum;
        private String subBU;
        private String subbu;
        private String docNo;
        private String cusCode;
        private String totalPrice;
        private String productPrice;
        private String subBUCode;
        private String subBUName;
        private String level2;
        private String defaultUnit;
        private boolean choose;
        //当前数量
        private String nowqty = "";
        private String lots = "";
        private int type;
        private String packageType = "";
        private String packageNum = "";
        private String packageUom = "";
        private String productId = "";
        private String domDate = "";
        private String dom_date = "";
        private String expDate = "";
        private String productDate;
        private String whName = "";
        private String whCode = "";
        private String exp_date;
        private String distCode;
        private String distName;
        private String cusName;
        private int arror;

        public static List<ProductItem> parseListIn(String array) throws JSONException {
            List<ProductItem> list = new ArrayList<>();
            JSONArray jsonArray = new JSONArray(array);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                ProductItem item = new ProductItem();
                item.setMaterialName(obj.has("name") ? obj.getString("name") : "");
                item.setSerialNumber(obj.has("code") ? obj.getString("code") : "");
                item.setMaterialNumber(obj.getString("number"));
                item.setSubBU(obj.has("subBU") ? obj.getString("subBU") : "");
                item.setUom(obj.getString("unit"));
                item.setQty(obj.getString("num"));
                item.setNowqty(obj.getString("nowqty"));
                item.setMaterialName(obj.has("materialName") ? obj.getString("materialName") : "");

                item.setDomDate(obj.getString("domDate"));
                item.setExpDate(obj.getString("expDate"));
                item.setLots(obj.getString("lots"));
                item.setProductId(obj.has("productId") ? obj.getString("productId") : "");
                item.setDistCode(obj.has("distCode") ? obj.getString("distCode") : "");
                item.setDistName(obj.has("distName") ? obj.getString("distName") : "");
                list.add(item);
            }
            return list;
        }

        public static List<ProductItem> parseListOut(String array) throws JSONException {
            List<ProductItem> list = new ArrayList<>();
            JSONArray jsonArray = new JSONArray(array);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                ProductItem item = new ProductItem();
                item.setMaterialName(obj.has("name") ? obj.getString("name") : "");
                item.setSerialNumber(obj.has("code") ? obj.getString("code") : "");
                item.setMaterialNumber(obj.getString("number"));
                item.setSubBU(obj.has("subBU") ? obj.getString("subBU") : "");
                item.setUom(obj.getString("unit"));
                item.setQty(obj.getString("num"));
                item.setNowqty(obj.getString("nowqty"));
                item.setMaterialName(obj.has("materialName") ? obj.getString("materialName") : "");

                item.setDomDate(obj.getString("domDate"));
                item.setExpDate(obj.getString("expDate"));
                item.setLots(obj.getString("lots"));
                item.setProductId(obj.has("productId") ? obj.getString("productId") : "");
                item.setWhName(obj.getString("whName"));
                item.setWhCode(obj.getString("whCode"));
                item.setPackageNum(obj.has("packageNum") ? obj.getString("packageNum") : "");
                item.setPackageUom(obj.has("packageUom") ? obj.getString("packageUom") : "");
                item.setPackageType(obj.has("packageType") ? obj.getString("packageType") : "");
                list.add(item);
            }
            return list;
        }

        public static String ListToString(List<ProductItem> list, boolean inFlag) {
            JSONArray array = new JSONArray();
            for (ProductItem item : list) {
                try {
                    if (inFlag) {
                        array.put(item.toJSONIn());
                    } else {
                        array.put(item.toJSONOut());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return array.toString();
        }

        public String getProductPrice() {
            if (TextUtils.isEmpty(productPrice)) {
                productPrice = "0.0";
            }
            return String.valueOf(Double.parseDouble(productPrice));
        }

        public void setProductPrice(String productPrice) {
            this.productPrice = productPrice;
        }

        public String getTotalPrice() {
            if (TextUtils.isEmpty(totalPrice)) {
                totalPrice = "0.0";
            }
            return String.valueOf(Double.parseDouble(totalPrice));
        }

        public void setTotalPrice(String totalPrice) {
            this.totalPrice = totalPrice;
        }

        public String getDocNo() {
            return docNo;
        }

        public void setDocNo(String docNo) {
            this.docNo = docNo;
        }

        public String getCusCode() {
            return cusCode;
        }

        public void setCusCode(String cusCode) {
            this.cusCode = cusCode;
        }

        public String getSubbu() {
            return subbu;
        }

        public void setSubbu(String subbu) {
            this.subbu = subbu;
        }

        public String getCusName() {
            return cusName;
        }

        public void setCusName(String cusName) {
            this.cusName = cusName;
        }

        public String getPkg() {
            return pkg;
        }

        public void setPkg(String pkg) {
            this.pkg = pkg;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getDom_date() {
            if (TextUtils.isEmpty(dom_date)) {
                return "";
            }
            return dom_date;
        }

        public void setDom_date(String dom_date) {
            this.dom_date = dom_date;
        }

        public String getLicense_no() {
            return license_no;
        }

        public void setLicense_no(String license_no) {
            this.license_no = license_no;
        }

        public String getBatchNumber() {
            return batchNumber;
        }

        public void setBatchNumber(String batchNumber) {
            this.batchNumber = batchNumber;
        }

        public String getSubBUCode() {
            return subBUCode;
        }

        public void setSubBUCode(String subBUCode) {
            this.subBUCode = subBUCode;
        }

        public String getSubBUName() {
            if (level2 != null) {
                return level2;
            }
            return subBUName;
        }

        public void setSubBUName(String subBUName) {
            this.subBUName = subBUName;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getDistCode() {
            return distCode;
        }

        public void setDistCode(String distCode) {
            this.distCode = distCode;
        }

        public String getDistName() {
            return distName;
        }

        public void setDistName(String distName) {
            this.distName = distName;
        }

        public String getMaterial_name() {
            return material_name;
        }

        public void setMaterial_name(String material_name) {
            this.material_name = material_name;
        }

        public String getExp_date() {
            return exp_date;
        }

        public void setExp_date(String exp_date) {
            this.exp_date = exp_date;
        }

        public String getWhName() {
            return whName;
        }

        public void setWhName(String whName) {
            this.whName = whName;
        }

        public String getWhCode() {
            return whCode;
        }

        public void setWhCode(String whCode) {
            this.whCode = whCode;
        }

        public String getProductDate() {
            return productDate;
        }

        public void setProductDate(String productDate) {
            this.productDate = productDate;
        }

        public String getPackageUom() {
            if (TextUtils.isEmpty(packageUom)) {
                return "";
            }
            return packageUom;
        }

        public void setPackageUom(String packageUom) {
            this.packageUom = packageUom;
        }

        public String getPackageType() {
            if (TextUtils.isEmpty(packageType)) {
                return "";
            }
            return packageType;
        }

        public void setPackageType(String packageType) {
            this.packageType = packageType;
        }

        public String getPackageNum() {
            if (TextUtils.isEmpty(packageNum)) {
                return "";
            }
            return packageNum;
        }

        public void setPackageNum(String packageNum) {
            this.packageNum = packageNum;
        }

        public String getLots() {
            if (TextUtils.isEmpty(lots)) {
                return "";
            }
            return lots;
        }

        public void setLots(String lots) {
            this.lots = lots;
        }

        public String getProductId() {
            return productId;
        }

        public void setProductId(String productId) {
            this.productId = productId;
        }

        public String getDomDate() {
            return domDate;
        }

        public void setDomDate(String domDate) {
            this.domDate = domDate;
        }

        public String getExpDate() {
            return expDate;
        }

        public void setExpDate(String expDate) {
            this.expDate = expDate;
        }

        public String getNowqty() {
            if (TextUtils.isEmpty(nowqty)) {
                nowqty = "0";
            }
            return nowqty;
        }

        public void setNowqty(String nowqty) {
            this.nowqty = nowqty;
        }

        public boolean isChoose() {
            return choose;
        }

        public void setChoose(boolean choose) {
            this.choose = choose;
        }

        public String getDefaultUnit() {
            return defaultUnit;
        }

        public void setDefaultUnit(String defaultUnit) {
            this.defaultUnit = defaultUnit;
        }

        public String getSubBU() {
            return subBU;
        }

        public void setSubBU(String subBU) {
            this.subBU = subBU;
        }

        public String getSerialNumber() {
            if (serialNumber == null) {
                return "";
            }
            return serialNumber;
        }

        public void setSerialNumber(String serialNumber) {
            this.serialNumber = serialNumber;
        }

        public String getMaterialName() {
            return materialName;
        }

        public void setMaterialName(String materialName) {
            this.materialName = materialName;
        }

        public String getMaterialNumber() {
            if (TextUtils.isEmpty(materialNumber)) {
                return "";
            }
            return materialNumber;
        }

        public void setMaterialNumber(String materialNumber) {
            this.materialNumber = materialNumber;
        }

        public String getLevel2() {
            return level2;
        }

        public void setLevel2(String level2) {
            this.level2 = level2;
        }

        public String getSaleUnit() {
            if (TextUtils.isEmpty(saleUnit)) {
                return "";
            }
            return saleUnit;
        }

        public void setSaleUnit(String saleUnit) {
            this.saleUnit = saleUnit;
        }

        public String getProductNum() {
            if (TextUtils.isEmpty(productNum)) {
                productNum = "0";
            }
            return productNum;
        }

        public void setProductNum(String productNum) {
            this.productNum = productNum;
        }

        public String getQty() {
            if (TextUtils.isEmpty(qty)) {
                qty = "0";
            }
            return qty;
        }

        public void setQty(String qty) {
            this.qty = qty;
        }

        public String getUom() {
            return uom;
        }

        public void setUom(String uom) {
            this.uom = uom;
        }

        public String getItemCode() {
            if (TextUtils.isEmpty(itemCode)) {
                if (!TextUtils.isEmpty(lots)) {
                    return lots;
                }
                if (!TextUtils.isEmpty(materialNumber)) {
                    return materialNumber;
                }
                if (!TextUtils.isEmpty(batchNumber)) {
                    return batchNumber;
                }
                return "";
            }
            return itemCode;
        }

        public void setItemCode(String itemCode) {
            this.itemCode = itemCode;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public JSONObject toJSONIn() throws JSONException {
            JSONObject object = new JSONObject();
            object.put("name", materialName);
            //序列号
            object.put("code", serialNumber);
            //批号
            object.put("number", materialNumber);
            object.put("materialName", materialName);

            object.put("subBU", subBU);
            object.put("unit", uom);
            object.put("num", qty);
            object.put("nowqty", nowqty);

            object.put("productId", productId);
            object.put("expDate", expDate);
            object.put("domDate", domDate);
            object.put("lots", lots);
            object.put("distCode", distCode);
            object.put("distName", distName);
//            object.put("lots", lots);
            return object;
        }

        public JSONObject toPostJSONIn() throws JSONException {
            JSONObject object = new JSONObject();
            //序列号
            object.put("serialNumber", serialNumber);
            object.put("itemCode", materialNumber);
            object.put("lots", lots);
            object.put("productId", productId);
            object.put("expDate", expDate);
            object.put("domDate", domDate);
            object.put("uom", uom);
            object.put("qty", nowqty);
            return object;
        }

        public JSONObject toJSONOut() throws JSONException {
            JSONObject object = new JSONObject();
            object.put("name", materialName);
            //序列号
            object.put("code", serialNumber);
            //批号
            object.put("number", materialNumber);
            object.put("materialName", materialName);
            object.put("subBU", subBU);
            object.put("unit", uom);
            object.put("num", qty);
            object.put("nowqty", nowqty);
            object.put("productId", productId);
            object.put("expDate", expDate);
            object.put("domDate", domDate);
            object.put("lots", lots);
            object.put("whCode", whCode);
            object.put("whName", whName);
            object.put("packageType", packageType);
            object.put("packageNum", packageNum);
            object.put("packageUom", packageUom);
            return object;
        }

        public JSONObject toPostJSONOut() throws JSONException {
            JSONObject object = new JSONObject();
            //序列号
            object.put("serialNumber", serialNumber);
            object.put("itemCode", materialNumber);
            object.put("lots", lots);
            object.put("productId", productId);
            object.put("expDate", expDate);
            object.put("domDate", domDate);
            object.put("uom", uom);
            object.put("qty", nowqty);
            object.put("whCode", whCode);
            object.put("whName", whName);
            object.put("packageType", packageType);
            object.put("packageNum", packageNum);
            object.put("packageUom", packageUom);
            return object;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null)
                return false;
            if (this == o)
                return true;
            if (getClass() != o.getClass())
                return false;
            ProductItem other = (ProductItem) o;
            if (!TextUtils.isEmpty(getSerialNumber())) {
                return other.getSerialNumber().equals(getSerialNumber());
            } else {
                return (other.getMaterialNumber() + ":" + other.getLots()).equals(getMaterialNumber() + ":" + getLots());
            }
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public int getArror() {
            return arror;
        }

        public void setArror(int arror) {
            this.arror = arror;
        }
    }
}
